package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.PhysicalService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/physical")
public class PhysicalController {
    @Reference
    private PhysicalService physicalService;

    @Autowired
    private JedisPool jedisPool;
//删除单个
    @RequestMapping("/deleteById")
    public Result delete(Integer id){

        try {
            physicalService.deleteById(id);
            return new Result(true,"删除成功你很牛b");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }

    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return physicalService.findPage(queryPageBean);
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(Integer[] ids) {

        try {
            physicalService.deleteByIds(ids);
            return new Result(true, "成功");
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }

    }
    @RequestMapping("/upload")
    public Result upload(@RequestParam("pdfFile") MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i - 1);
        String fileName= UUID.randomUUID().toString()+ substring;
        try {
            QiniuUtils.upload2Qiniu(file.getBytes(),fileName);
            jedisPool.getResource().sadd("PDF",fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"上传失败");
        }
        return new Result(true, "上传成功",fileName);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        try {
            physicalService.add(map);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            return new Result(false,"新增失败");
        }
        return new Result(true,"新增成功");

    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map){
        try {
            physicalService.edit(map);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);

    }

    @RequestMapping("/findAll")
    public Result findAll(){

        try{
            List<Map> list = physicalService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
